/*
 * FontMeasurer.java
 *
 * Created on 25 november 2005, 10:36
 *
 * @author wijnand.schepens@hogent.be
 */

package org.jfonia.assets.fonts;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;

public class FontMeasurer {
    
    public FontMeasurer() {
    	
    }
    
    /**
     * measure the graphical size of a string drawn on a Graphics context using a font
     *
     * notes:
     * - FontMetrics.getAscent and getDescent give the "typical" ascent,descent for "any" line of text
     *   Here, the actual glyph(s) are measured
     * - getBounds returns Rectangle (too crude)
     */
    public static Rectangle2D getBounds2D(Graphics2D g2, Font font, String text)
    {
            FontRenderContext frc = g2.getFontRenderContext();
            if (text==null || text.length()==0)
            	return new Rectangle2D.Double(0,0,0,0);
            TextLayout tl = new TextLayout(text, font, frc);
            Shape outline = tl.getOutline(null);
            //g2.translate(x,y);
            //g2.draw(outline);   
            return outline.getBounds2D();
    }
}
